﻿using Shinva.LADModel.Abstractions;
using Shinva.LADModel.Models;
using Shinva.Variables.Interface;
using Shinva.Variables.Models;
using Shinva.Variables.Type.bit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Shinva.LADModel.Type.BitLogicOperations
{
    /// <summary>
    /// 这是触点节点,可以为常开或者常闭
    /// </summary>
    public class Contact : ElementNodeBase
    {


        /// <summary>
        /// 触点的操作数,指向实际的一个变量表中的一个变量，此处用于快速访问
        /// </summary>
        public PlcBool operand { get; set; } 


        /// <summary>
        /// 触点值是否取反,取反代表常闭触点
        /// </summary>
        public bool Negated { get; set; } = false;



        public override PlcBool ENO
        {
            get
            {
                eno.Value = !Negated && operand==null?false:operand.Value && en.Value;
                return eno;
            }
            set
            {
                eno = value;
            }
        }
        public override PlcBool EN
        {
            get
            {
                return en;
            }
            set
            {
                en = value;
            }
        }

      
        public Contact()
        {
            Name = "Contact";
            //在这里要注册一个触点的操作数，input类型，如果不传入实例就默认为空
            variableTableInterface.AddVariable("operand", StorageType.Input);

            //赋值给operand属性
            operand = variableTableInterface.GetVariableValue<PlcBool>("operand");
        }


        public override IPlcMember GetInterfaceVariableByName(string name)
        {
            if (name == "in") return EN;
            else if (name == "out") return ENO;
            else return variableTableInterface.GetVariableValue<IPlcMember>(name);
        }
    }
}
